package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.RegisteredItemMapper;
import com.hghivln.pojo.domain.RegisteredItem;
import com.hghivln.pojo.dto.RegisteredItemDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.RegisteredItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class RegisteredItemServiceImpl implements RegisteredItemService{
    @Autowired
    private RegisteredItemMapper registeredItemMapper;

    @Override
    public AjaxResult listRegisteredItemForPage(RegisteredItemDto registeredItemDto) {
        Page<RegisteredItem> page = new Page<>(registeredItemDto.getPageNum(), registeredItemDto.getPageSize());
        QueryWrapper<RegisteredItem> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(registeredItemDto.getRegItemName()),RegisteredItem.COL_REG_ITEM_NAME,registeredItemDto.getRegItemName());
        qw.eq(StringUtils.isNotBlank(registeredItemDto.getStatus()),RegisteredItem.COL_STATUS,registeredItemDto.getStatus());
        qw.eq(RegisteredItem.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.orderByAsc(RegisteredItem.COL_CREATE_TIME);
        registeredItemMapper.selectPage(page,qw);
        return AjaxResult.success("查询成功",page.getRecords(),page.getTotal());
    }

    @Override
    public AjaxResult addRegisteredItem(RegisteredItemDto registeredItemDto) {
        RegisteredItem registeredItem = new RegisteredItem();
        BeanUtils.copyProperties(registeredItemDto,registeredItem);
        registeredItem.setCreateBy(registeredItemDto.getSimpleUser().getUserName());
        registeredItem.setCreateTime(DateUtil.date());
        return AjaxResult.success(registeredItemMapper.insert(registeredItem));
    }

    @Override
    public AjaxResult updateRegisteredItem(RegisteredItemDto registeredItemDto) {
        RegisteredItem registeredItem = new RegisteredItem();
        BeanUtils.copyProperties(registeredItemDto,registeredItem);
        registeredItem.setUpdateBy(registeredItemDto.getSimpleUser().getUserName());
        return AjaxResult.success(registeredItemMapper.updateById(registeredItem));
    }

    @Override
    public AjaxResult deleteRegisteredItemByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        if (idList.size()>0) {
            return AjaxResult.success(registeredItemMapper.deleteRegisteredItemByIds(idList));
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult selectRegisteredItemById(Long id) {
        QueryWrapper<RegisteredItem> qw = new QueryWrapper<>();
        qw.eq(RegisteredItem.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(RegisteredItem.COL_REG_ITEM_ID,id);
        return AjaxResult.success(registeredItemMapper.selectOne(qw));
    }

    @Override
    public AjaxResult selectAllRegisteredItem() {
        QueryWrapper<RegisteredItem> qw = new QueryWrapper<>();
        qw.eq(RegisteredItem.COL_DEL_FLAG,Constants.DEL_FALSE);
        qw.eq(RegisteredItem.COL_STATUS,Constants.STATUS_TRUE);
        return AjaxResult.success(registeredItemMapper.selectList(qw));
    }
}
